#!/bin/bash
# 作者：施罗伟
# 时间：2018-01-20
# 描述：青岛新思科Linux课程SHELL实验
# 功能方法：
# 1 安装相关的yum源
# 2 安装必要的工具软件
# 3 安装必要的库文件

source /etc/rc.d/init.d/functions
source ./common/common_func.sh

LOG_PATH='./install.log'

#安装yum源
function installYum(){
	(yum install -y wget \
	&& cd /etc/yum.repos.d/ \
	&& [ -d "./backup" ] && mv ./CentOS-*.repo ./backup || (mkdir ./backup;mv ./CentOS-*.repo ./backup) \
	&& wget -O /etc/yum.repos.d/ali.repo http://mirrors.aliyun.com/repo/Centos-7.repo -o /dev/null \
	&& yum install -y epel-release \
	&& yum clean all&& yum makecache)  > ${LOG_PATH}
	# 日志输出到日记文件中
	REVALUE=$?
	[[ ${REVALUE} == 0 ]] && action "安装yum源成功" /bin/true || action "安装yum源" /bin/false
	exitShell ${REVALUE}

}

function installTest(){
	echo
	echo "this is test!!!"
}

#删除安装的yum源操作
function uninstallYum(){
	cd /etc/yum.repos.d/
	mv ali.repo /tmp && mv epel*.repo /tmp
	yum remove -y epel-release > /dev/null 2>&1
	yum remove -y wget > /dev/null 2>&1
	mv ./backup/* ./ && mv ./backup /tmp
	rm -rf /tmp/*
}

# ssh安全优化
function strongssh(){
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak 

sed -i 's/#Port 22/Port 66222/g' /etc/ssh/sshd_config
 sed -i 's/#PermitEmptyPasswords no/PermitEmptyPasswords no/g' /etc/ssh/sshd_config
 sed -i 's/#PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
 sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
 sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
systemctl restart sshd
REVALUE=$?
	[[ ${REVALUE} == 0 ]] && action "修改ssh默认参数完成" /bin/true || action "修改ssh默认参数失败" /bin/false
	exitShell ${REVALUE}
}
# 恢复ssh
function recoveryssh(){
mv /etc/ssh/sshd_config /tmp
mv /etc/ssh/sshd_config.bak  /etc/ssh/sshd_config
systemctl restart sshd 2>/dev/null

}
# 安装必要的工具软件
function installTools(){
	yum install -y \
	sysstat \
	ntpdate \
	wget \
	bash-completion \
	vim-enhanced \
	net-tools \
	lsof \
	lrzsz \
	tree > /dev/null 2>&1
}
# 安装必要的库文件
function installDevels(){
	yum install -y \
	gcc \
	cmake \
	bzip2-devel \
	curl-devel \
	db4-devel \
	libjpeg-devel \
	libpng-devel \
	freetype-devel \
	libXpm-devel \
	gmp-devel \
	libc-client-devel \
	openldap-devel \
	unixODBC-devel \
	postgresql-devel \
	sqlite-devel \
	aspell-devel \
	net-snmp-devel \
	libxslt-devel \
	libxml2-devel \
	pcre-devel \
	mysql-devel \
	pspell-devel \
	libmemcached \
	libmemcached-devel \
	zlib-devel > /dev/null 2>&1
}





# 安装依赖
LOG_PATH='./install.log'
OPENRESTY_CONFIG_FILES='/usr/local/openresty/nginx/conf/nginx.conf'

REVALUE=0
function installdepends(){
	yum  -y   install pcre-devel openssl-devel gcc curl yum-utils >> ${LOG_PATH}
	# 日志输出到日记文件中
	REVALUE=$?
	[[ ${REVALUE} == 0 ]] && action "安装依赖成功" /bin/true || action "安装依赖" /bin/false
	exitShell ${REVALUE}
}

# 配置openresty yum源
function openrestyyum(){
	yum-config-manager --add-repo https://openresty.org/yum/cn/centos/OpenResty.repo >> ${LOG_PATH}
}


# 安装openresty
REVALUE=0
function installopenresty(){
	(yum -y install openresty \
		&&systemctl start openresty
		)>> ${LOG_PATH}
	REVALUE=$?
	[[ ${REVALUE} == 0 ]] && action "安装openresty成功" /bin/true || action "安装openresty" /bin/false
	exitShell ${REVALUE}
}

# 修改配置文件
REVALUE=0
function configureopenresty(){
	cp -r $OPENRESTY_CONFIG_FILES $OPENRESTY_CONFIG_FILES.backup-`date +%Y%m%d%H%M` \
	&&sed -i "s/localhost/abc.com/g" $OPENRESTY_CONFIG_FILES \
	&&openresty -t \
	&&echo "192.168.1.120 abc.com" >> /etc/hosts \
	&&systemctl restart openresty  >> ${LOG_PATH}
	REVALUE=$?
	[[ ${REVALUE} == 0 ]] && action "修改配置成功" /bin/true || action "修改配置" /bin/false
	exitShell ${REVALUE}
}

# 卸载openresty
function uninstallopenresty(){
	(systemctl stop openresty \
	&&yum -y autoremove openresty) >> ${LOG_PATH}
}